package com.example.dao;

import com.example.entity.Vote;
import com.example.entity.VoteMeg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 陈俊杰
 */
@Component
@Slf4j
public class VoteDao {

    @Autowired
    @Qualifier("apiJdbcTemplate")
    protected JdbcTemplate jdbcTemplate;

    /**
     * 创建一个投票
     * @param vote 投票信息
     * @param voteList 参与投票人员
     * @return 投票信息ID
     */
    public String addVote(VoteMeg vote, List<Vote> voteList){
        String SQL = String.format("insert into vote_meg values('%s','%s','%s','%s','%s','%s')",vote.getId(),vote.getMessage(),vote.getStatus(),vote.getStartTime(),
                vote.getEndTime(),vote.getNumber());
        jdbcTemplate.execute(SQL);
        log.info(SQL);
        for (Vote v:
             voteList) {
            SQL = String.format("insert into vote values('%S','%s','%s',0)",vote.getId(),v.getStudentId(),
                    v.getName());
            jdbcTemplate.execute(SQL);
        }
        return vote.getId();
    }

    /**
     * 遍历投票的学生
     *
     * @return
     */
    public List queryAllVote(){
        String SQL = String.format("select * from vote");
        log.info(SQL);
        RowMapper rowMapper = new BeanPropertyRowMapper(Vote.class);
        return jdbcTemplate.query(SQL,rowMapper);
    }

    /**
     * 学生投票
     * @param student_id
     * @param count
     * @return
     */
    public boolean Vote(String student_id,String count){
        try {
            String SQL = String.format("update vote set count=%s where student_id=%s", count+1, student_id);
            log.info(SQL);
            jdbcTemplate.update(SQL);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查询所有投票信息
     * @return 返回投票信息集合
     */
    public List<VoteMeg> getAllVote(){
        String SQL = "select * from vote_meg";
        List<VoteMeg> query = jdbcTemplate.query(SQL, new BeanPropertyRowMapper<VoteMeg>(VoteMeg.class));
        return query.size()>0||query!=null?query:null;
    }

    /**
     * 根据投票ID查询一次投票记录
     * @param id 投票信息ID
     * @return 返回投票记录
     */
    public List<Vote> getOneVote(String id){
        String SQL = String.format("select * from vote where id='%s'",id);
        List<Vote> query = jdbcTemplate.query(SQL, new BeanPropertyRowMapper<>(Vote.class));
        return query.size()>0||query!=null?query:null;
    }

    /**
     * 截止投票
     * @param id
     * @return
     */
    public void offVote(String id){
        String SQL = String.format("update vote_meg set status = '已截止' where id='%S'",id);
        jdbcTemplate.execute(SQL);
        SQL = String.format("select * from vote_meg where id='%s'",id);
        List<VoteMeg> query = jdbcTemplate.query(SQL, new BeanPropertyRowMapper<VoteMeg>(VoteMeg.class));
        SQL = String.format("select * from vote where id='%s' order by count desc",id);
        List<Vote> query1 = jdbcTemplate.query(SQL, new BeanPropertyRowMapper<>(Vote.class));
        int i = 0;
        int p =Integer.parseInt(query.get(0).getNumber());
        for (Vote v:
             query1) {
            if(i<p){
                SQL=String.format("update result set vote_result='1',intermediate_result='1' where id='%s' and student_id = '%s'",
                        id,v.getStudentId());
            }else{
                SQL=String.format("update result set vote_result='0',intermediate_result='0' where id='%s' and student_id = '%s'",
                        id,v.getStudentId());
            }
            jdbcTemplate.execute(SQL);
            i++;
        }
    }
}
